package com.ruoyi.interviews.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.interviews.mapper.InterviewsMapper;
import com.ruoyi.system.domain.Applications;
import com.ruoyi.system.service.IApplicationsService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.work.domain.Jobbasicinfo;
import com.ruoyi.work.service.IJobbasicinfoService;
import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.interviews.domain.Interviews;
import com.ruoyi.interviews.service.IInterviewsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 面试记录Controller
 *
 * @author ruoyi
 * @date 2024-11-10
 */
@RestController
@RequestMapping("/interviews/interviews")
public class InterviewsController extends BaseController
{
    @Autowired
    private IInterviewsService interviewsService;
    @Autowired
    private IJobbasicinfoService jobbasicinfoService;
    @Autowired
    private IApplicationsService applicationsService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private InterviewsMapper interviewsMapper;

    /**
     * 查询面试记录列表
     */
    @PreAuthorize("@ss.hasPermi('interviews:interviews:list')")
    @GetMapping("/list")
    public TableDataInfo list(Interviews interviews)
    {
        List<Interviews> list = interviewsService.selectInterviewsList(interviews);
        for(Interviews interviews1:list){
            Applications applications=applicationsService.selectApplicationsById(interviews1.getApplicationId());
            Jobbasicinfo jobbasicinfo= jobbasicinfoService.selectJobbasicinfoByJobId(applications.getJobId());
            interviews1.setCompanyName(jobbasicinfo.getCompanyName());
            interviews1.setJobTitle(jobbasicinfo.getJobTitle());
            String name=interviewsMapper.getUsername(interviews1.getApplicationId());
            interviews1.setUserName(name);
        }
        return pagination(list);
    }

    /**
     * 导出面试记录列表
     */
    @PreAuthorize("@ss.hasPermi('interviews:interviews:export')")
    @Log(title = "面试记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Interviews interviews)
    {
        String name=SecurityUtils.getUsername();
        List<Interviews> list = interviewsService.selectInterviewsList(interviews);
        for(Interviews interviews1:list){
            Applications applications=applicationsService.selectApplicationsById(interviews1.getApplicationId());
            Jobbasicinfo jobbasicinfo= jobbasicinfoService.selectJobbasicinfoByJobId(applications.getJobId());
            interviews1.setCompanyName(jobbasicinfo.getCompanyName());
            interviews1.setJobTitle(jobbasicinfo.getJobTitle());
            interviews1.setUserName(name);
        }
        ExcelUtil<Interviews> util = new ExcelUtil<Interviews>(Interviews.class);
        util.exportExcel(response, list, "面试记录数据");
    }

    /**
     * 获取面试记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('interviews:interviews:query')")
    @GetMapping(value = "/{interviewId}")
    public AjaxResult getInfo(@PathVariable("interviewId") Long interviewId)
    {
       Interviews interviews=interviewsService.selectInterviewsByInterviewId(interviewId);
       Applications applications=applicationsService.selectApplicationsById(interviews.getApplicationId());
       Long userid=applications.getUserId();
       Long jobid=applications.getJobId();
        SysUser sysUser=iSysUserService.selectUserById(userid);
        interviews.setUserName(sysUser.getUserName());
        Jobbasicinfo jobbasicinfo= jobbasicinfoService.selectJobbasicinfoByJobId(jobid);
        interviews.setJobTitle(jobbasicinfo.getJobTitle());
        interviews.setCompanyName(jobbasicinfo.getCompanyName());
       return success(interviews);
    }

    /**
     * 新增面试记录
     */
    @PreAuthorize("@ss.hasPermi('interviews:interviews:add')")
    @Log(title = "面试记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Interviews interviews)
    {
        return toAjax(interviewsService.insertInterviews(interviews));
    }

    /**
     * 修改面试记录
     */
    @PreAuthorize("@ss.hasPermi('interviews:interviews:edit')")
    @Log(title = "面试记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Interviews interviews)
    {
        return toAjax(interviewsService.updateInterviews(interviews));
    }

    /**
     * 删除面试记录
     */
    @PreAuthorize("@ss.hasPermi('interviews:interviews:remove')")
    @Log(title = "面试记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{interviewIds}")
    public AjaxResult remove(@PathVariable Long[] interviewIds)
    {
        return toAjax(interviewsService.deleteInterviewsByInterviewIds(interviewIds));
    }
    /**
     * 定时清空
     */
    /**
     * 删除所有的面试记录
     */
    @PreAuthorize("@ss.hasPermi('interviews:interviews:deleteAll')")
    @Log(title = "面试记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteAll")
    public AjaxResult deleteAll()
    {
        return toAjax(interviewsService.deleteAll());
    }
//    @PreAuthorize("@ss.hasPermi('interviews:interviews:remove')")
//    @Log(title = "面试记录", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{interviewIds}")
//    public AjaxResult deletetime(List<Applications> applications)
//    {
//        interviewsService.selectday();
//        return toAjax(interviewsService.(applications));
//    }


}
